<?php

namespace App\Repositories\Home;

use App\Models\Admin\CategoryModel;
use App\Models\Admin\JobModel;
use App\Models\Admin\StaffModel;
use App\Models\Admin\StaffOptionModel;
use App\Models\Admin\UserModel;
use App\Repositories\BaseRepository;

class StaffRepository extends BaseRepository
{
    protected $user;
    protected $staff;
    protected $option;
    protected $cate;
    protected $job;

    public function __construct()
    {
        $this->user = new UserModel();
        $this->staff = new StaffModel();
        $this->option = new StaffOptionModel();
        $this->cate = new CategoryModel();
        $this->job = new JobModel();
    }

    /**
     * 获取web工人搜索条件
     */
    public function getWebConditions()
    {
        //学历
        $eduArr = array();
        $edus = $this->staff->getEdus();
        $eduArr[0]['eduId'] = '0';
        $eduArr[0]['eduName'] = '所有';
        foreach ($edus as $eduKey => $edu) {
            $eduArr[] = [
                'eduId' => $eduKey,
                'eduName' => $edu,
            ];
        }
        //岗位
        $jobs = $this->job->where('del',0)->get();
        $jobArr = array();
        if (count($jobs)) {
            foreach ($jobs as $job) {
                $jobArr[] = [
                    'jobId' => $job->id,
                    'jobName' => $job->name,
                ];
            }
        }
//        //技能
//        $skills = $this->cate->getSubsByCates(129);
//        //年限
//        $years = $this->cate->getSubsByCates(136);
        return [
            'eduList' => $eduArr,
            'jobList' => $jobArr,
//            'skillList' => $skills,
//            'yearList' => $years,
        ];
    }

    /**
     * 获取web工人列表
     * @param $eduId 学历ID
     * @param $jobId 岗位ID
     * @param $page 页码
     * @param $pageSize 每页记录数
     */
    public function getWebStaffList($eduId,$jobId,$page,$pageSize)
    {
        $query = $this->staff->where('uid','>',0);
        //搜索条件判断
        if ($eduId) { $query = $query->where('education',$eduId); }
        if ($jobId) { $query = $query->where('job',$jobId); }
        //分页处理
        $limit = $pageSize ? $pageSize : $this->limit;
        $pageStart = ($page - 1) * $limit;
        $models = $query->skip($pageStart)->take($limit)->get();
        $dataArr = array();
        if (!count($models)) { return $dataArr; }
        foreach ($models as $model) {
            $userOne = $this->user->where('id',$model->uid)->first();
            $userName = $userOne ? $userOne->name : '未定义';
            $head = $userOne ? $userOne->head : '';
            $jobId = $model ? $model->job : 0;
            $jobOne = $this->job->where('id',$jobId)->first();
            $jobName = $jobOne ? $jobOne->name : '未定义';
            $dataArr[] = [
                'name' => $userName,
                'head' => $head,
                'jobName' => $jobName,
            ];
        }
        return $dataArr;
    }

    /**
     * 获取详情:
     */
    public function getOne($uid)
    {
        $model = $this->staff->where('uid',$uid)->first();
        $userOne = $this->user->where('id',$uid)->first();
        $jobId = $model ? $model->job : 0;
        $jobOne = $this->job->where('id',$jobId)->first();
        $userName = $userOne ? $userOne->name : '未定义';
        $head = $userOne ? $userOne->head : '';
        $jobName = $jobOne ? $jobOne->name : '未定义';
        $edus = $this->staff->getEdus();
        $eduId = $model ? $model->education : 0;
        $nativePlace = $model ? $model->native_place : '未定义';
        $house = $model ? $model->house : '未定义';
        $image = $model ? $model->image : '';
        $eduName = array_key_exists($eduId,$edus) ? $edus[$eduId] : '未定义';
        return array(
            'name' => $userName,
            'jobName' => $jobName,
            'head' => $head,
            'eduName' => $eduName,
            'nativePlace' => $nativePlace,
            'house' => $house,
            'image' => $image,
        );
    }
}